import {
    UserRegister,
    SendUserCode,
} from "~/api";

let codeMaxSecond = 0;
let codeInterval = null;

const infoVerify = ({ phone, code, password, birthday }) => {
    if (isEmpty(phone)) {
        Message().warning("请输入手机号");
        return false;
    }

    if (!isPhone(phone)) {
        Message().warning("请输入正确的手机号");
        return false;
    }

    if (isEmpty(code)) {
        Message().warning("请输入验证码");
        return false;
    }

    if (isEmpty(password)) {
        Message().warning("请输入密码");
        return false;
    }

    if (password.length < 6 || password.length > 16) {
        Message().warning("请输入6到16位密码");
        return false;
    }

    if (birthday == "") {
        Message().warning("请选择出生日期");
        return false;
    }

    return true;
};

export const useContent = () => {
    const userStore = useUserStore();
    const num = ref(86);
    const phone = ref("");
    const password = ref("");
    const codeText = ref("获取验证码");
    const code = ref("");
    const birthday = ref("");
    const experience_code = ref("");

    const onSendCodeSuccess = () => {
        const _func = () => {
            if (codeMaxSecond === 0) {
                codeText.value = `获取验证码`;
                clearInterval(codeInterval);
            } else {
                codeText.value = `${codeMaxSecond--}s`;
            }
        };
        codeMaxSecond = 120;
        codeInterval = setInterval(_func, 1000);
        _func();
    };

    const onSendCode = async () => {
        if (codeMaxSecond > 0) return;
        if (isPhone(phone.value)) {
            const res = await SendUserCode({
                phone: phone.value,
                event: "register",
            });

            if (res.code == 200) {
                Message().success("发送成功");
                onSendCodeSuccess();
            } else {
                Message().error(res.msg);
            }
        } else {
            Message().warning("请输入正确的手机号");
        }
    };

    const onLogin = () => navigateTo('/login.html');

    const onForgetPwd = () => navigateTo("/forgetPwd");

    const onRegister = async () => {
        const verify = infoVerify({
            phone: phone.value,
            code: code.value,
            password: password.value,
            birthday: birthday.value,
        });

        if (verify) {
            const res = await UserRegister({
                phone: phone.value,
                password: password.value,
                code: code.value,
                experience_code: experience_code.value,
                birthday: birthday.value,
            });

            if (res.code == 200) {
                navigateTo("/login");
                Message().success("注册成功");
            } else {
                Message().error(res.msg);
            }
        }
    };

    const onSubmit = () => {
        onRegister();
    };

    const birthdayDisabled = (date) => {
        return date > new Date();
    };

    return {
        num,
        birthday,
        phone,
        password,
        codeText,
        code,
        experience_code,
        birthdayDisabled,
        onSendCode,
        onLogin,
        onForgetPwd,
        onSubmit,
    };
};
